//
//  UserShareDesController.m
//  吃货·上海
//
//  Created by 高青松 on 15/11/25.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "UserShareDesController.h"

#import "UserDesHeaderView.h"// 自定义表头
#import "UserShareDesCell.h" // 自定义cell
#import "UserShareDesModel.h"
#import "CommentController.h" //评论
@interface UserShareDesController()

@property (nonatomic,strong)UserShareDesModel *desModel;

//评论数组
@property (nonatomic, strong)NSArray *desModelArray;
@end


static NSString *indentifier = @"cell";
@implementation UserShareDesController
//刷新
-(void)viewWillAppear:(BOOL)animated
{
    [self getDesModel];
}


- (void)viewDidLoad
{
    [self getDesModel];
     // 注册表头
    [self.tableView registerClass:[UserDesHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.separatorStyle = NO;
    
    // 注册cell
    [self.tableView registerClass:[UserShareDesCell class] forCellReuseIdentifier:indentifier];
}


#pragma mark tableView数据源方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.desModelArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UserShareDesCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    BmobObject *obj = self.desModelArray[indexPath.row];
    cell.model = [[UserShareDesModel alloc]initWithbmobObject:obj];
    
    return cell;
}
//点击cell方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentController *commentViewVc = [[CommentController alloc]init];
    commentViewVc.userShareModel = self.shareModel;
    [self.navigationController pushViewController:commentViewVc animated:NO];
}
//获取评论model
-(void)getDesModel
{
    BmobQuery *bqeury = [BmobQuery queryWithClassName:[NSString stringWithFormat:@"Comment%@%@",self.shareModel.userName, self.shareModel.objectId]];
    [bqeury orderByDescending:@"createdAt"];
    [bqeury findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error)
     {
         if (error)
         {
             NSLog(@"%@",error);
         }
         self.desModelArray = array;
         dispatch_async(dispatch_get_main_queue(), ^{
             [self.tableView reloadData];
         });
     }];
}


// 动态返回cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [UserShareDesCell heightForCell:self.desModel];
}

// 表头及其高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UserDesHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    [headerView setUserShareModel:self.shareModel];
    headerView.shareModel = self.shareModel;
    NSLog(@"shareModel.info ==== %@",headerView.shareModel.info);
    UITapGestureRecognizer *commentTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(commentTapAction:)];
    [headerView.commentBgView addGestureRecognizer:commentTap];
    return headerView;
    
}
//评论手势
-(void)commentTapAction:(UITapGestureRecognizer *)commentTap
{
    NSLog(@"表头评论");
    CommentController *commentViewVc = [[CommentController alloc]init];
    commentViewVc.userShareModel = self.shareModel;
    [self.navigationController pushViewController:commentViewVc animated:NO];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [UserDesHeaderView heightForCell:self.shareModel];
}

// 表头动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = [UserDesHeaderView heightForCell:self.shareModel];
    
    if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        
    } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
        
    }
    
}


@end
